import { defineStore } from "pinia";
import { ref } from "vue";
// 导入请求登录的接口
import { login } from "@/api/auth.js";
export const useUserStore = defineStore(
  "user",
  () => {
    //定义管理用户数据的state
    const userInfo = ref({ userName: "", avatar: "" });
    //定义登录状态
    const isAuth = ref(false);
    //获取登录状态
    const getAuth = () => {
      return isAuth;
    };
    //认证成功
    const authed = () => {
      isAuth.value = true;
    };
    //清除认证信息
    const removeAuth = () => {
      isAuth.value = false;
    };
    // 定义获取接口数据的action函数
    const getUserInfo = (userName, avatar) => {
      userInfo.value.userName = userName;
      userInfo.value.avatar = avatar;
      return userInfo
    };
     // 定义获取接口数据的action函数
     const getDeatailUserInfo = () => {
      return userInfo
    };
    // 退出时清除用户信息
    const clearUserInfo = () => {
      userInfo.value = {};
    };
    // 以对象的形式把state和action return出去
    return {
      userInfo,
      isAuth,
      getUserInfo,
      clearUserInfo,
      getAuth,
      authed,
      removeAuth,
      getDeatailUserInfo
    };
  },
  {
    persist: true,
  }
);
